package com.jk.smartelderlycare.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jk.smartelderlycare.dao.SysLabelMapper;
import com.jk.smartelderlycare.dao.sysVideoMapper;
import com.jk.smartelderlycare.dto.SysLabel;
import com.jk.smartelderlycare.dto.sysVideo;
import com.jk.smartelderlycare.pojo.PagedResponse;
import com.jk.smartelderlycare.service.SysVideoService;
import com.jk.smartelderlycare.utils.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author hhk
 * @version 1.0
 * @ClassName SysVideoServiceImpl
 * @program: smart-elderly-care
 * @description: TODO
 * @date 2024/11/15 8:54
 */
@Service
public class SysVideoServiceImpl implements SysVideoService {
    @Autowired
    private sysVideoMapper SysVideoMapper;

    @Autowired
    private SysLabelMapper sysLabelMapper;
    /**
     * @description: 后端分页查询
     * @param: [video, pageNum, pageSize]
     * @return: com.jk.smartelderlycare.pojo.PagedResponse<com.jk.smartelderlycare.dto.sysVideo>
     * @author hhk
     * @date: 2024/11/15 8:56
     */
    @Override
    public PagedResponse<sysVideo> findSysVideoPaginationList(sysVideo video, Integer pageNum, Integer pageSize) {
        Page<sysVideo> Page = new Page<>(pageNum,pageSize);
        IPage<sysVideo> sysVideoPageList = SysVideoMapper.findSysVideoPaginationList(video,Page);
        return PaginationUtil.getPagedResponse(sysVideoPageList.getRecords(),pageNum,pageSize,sysVideoPageList.getTotal());
    }
    /**
     * @description: 新增
     * @param: [video]
     * @return: void
     * @author hhk
     * @date: 2024/11/15 8:58
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addSysVideo(sysVideo video) {

        SysVideoMapper.insert(video);
        //新增中间表
        HashMap<String, Object> map = new HashMap<>();
        if(null!=video.getLabelIds()){
            //循环labelId
            for (Long labelId : video.getLabelIds()) {
                map.put("videoId",video.getVideoId());
                map.put("labelId",labelId);
                SysVideoMapper.addSysVideoAndLabel(map);
            }
        }

    }
    /**
     * @description: 根据id删除
     * @param: [videoId]
     * @return: void
     * @author hhk
     * @date: 2024/11/15 8:59
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteSysVideoByVideoId(Long videoId) {
        SysVideoMapper.deleteById(videoId);
        //删除中间表
        SysVideoMapper.deleteVideoAndLabelByVideoId(videoId);

    }
    /**
     * @description: 根据id回显
     * @param: [videoId]
     * @return: com.jk.smartelderlycare.dto.sysVideo
     * @author hhk
     * @date: 2024/11/15 8:59
     */
    @Override
    public sysVideo findSysVideoByVideoId(Long videoId) {
        //根据id查询用户信息
        sysVideo video = SysVideoMapper.findSysVideoByVideoId(videoId);
        if(null != video.getStrLabel() && !"".equals(video.getStrLabel())){
            String[] labelArr = video.getStrLabel().split(",");
            List<Long> labelList = new ArrayList<Long>();
            for (String labelId : labelArr) {
                labelList.add(Long.parseLong(labelId));
            }
            video.setLabelIds(labelList);
        }
        return video;
    }
    /**
     * @description: 修改
     * @param: [video]
     * @return: void
     * @author hhk
     * @date: 2024/11/15 8:59
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSysVideo(sysVideo video) {
        SysVideoMapper.updateById(video);
        //二、修改中间表：先删后增
        //1.根据videoId删除中间表
        SysVideoMapper.deleteVideoAndLabelByVideoId(video.getVideoId());
        //新增
        HashMap<String, Object> map = new HashMap<>();
        if(null!=video.getLabelIds()){
            //循环labelId
            for (Long labelId : video.getLabelIds()) {
                map.put("videoId",video.getVideoId());
                map.put("labelId",labelId);
                SysVideoMapper.addSysVideoAndLabel(map);
            }
        }
    }
    /**
     * @description: 标签动态下拉
     * @param: []
     * @return: java.util.List<com.jk.smartelderlycare.dto.SysLabel>
     * @author hhk
     * @date: 2024/11/15 8:59
     */
    @Override
    public List<SysLabel> findSysLabelList() {
        return sysLabelMapper.selectList(null);
    }
    /**
     * @description: 批量删除
     * @param: [ids]
     * @return: void
     * @author hhk
     * @date: 2024/11/15 11:10
     */
    @Override
    public void deleteSysVideoByIds(List<Integer> ids) {
        SysVideoMapper.deleteBatchIds(ids);
    }
    /**
     * @description: 导出
     * @param: []
     * @return: java.util.List<com.jk.smartelderlycare.dto.sysVideo>
     * @author hhk
     * @date: 2024/11/15 11:50
     */
    @Override
    public List<sysVideo> findSysVideoList() {
        return SysVideoMapper.findSysVideoList();
    }

}
